/*
//
//	Si este script es incluido en el header todos los formularios que no sean de la clase
//	css 'ignoreForm' son observados.
//	Si el usuario trata de cerrar la pagina sin guardar los cambios en algun formulario
//  observado imprimo un mensaje de advertencia.	
//
*/

// Mensaje que se imprime si intenta salir del form sin salvar
var warn_message = 'Hay cambios no guardados. Seguro que desea salir sin guardar?';

// Create class which will allow forms to monitor themselves for changes

var FormWatch = Class.create();

FormWatch.prototype = {
    initialize : function(form, options) {
        this.submitted = false;

        this.form = $(form);

        // Let's serialize this.form and store it...
        // This will store the "original" state of the form
        this.formcontents = $(form).serialize();

        // On submit, we let the user changes pass thru without prompting
        Event.observe(this.form, 'submit', function()
										{this.submitted = true; }.bind(this));

        // On beforeunload event, we check for changes using our confirmExit
        // method and prompt user to confirm, if necessary
        Event.observe(window, 'beforeunload', this.confirmExit.bind(this));
           // Event.stop(ev);
        },
        confirmExit : function(ev) {
            // Serialize the form contents again
            this.newcontents = this.form.serialize();

            // Compare latest form contents with original form contents
            // to see if anything has changed.
            if ((this.formcontents != this.newcontents) && ! (this.submitted)) {

               ev.returnValue = warn_message;
           // return Event.stop(ev);
        }
    }
}



// Utilizes Prototype JS lib
// On window load, loop thru forms on page which should not be ignored.
// Register these forms with our modification listener so we can prompt
// user to save changes. (see listener above)

 Event.observe(window, 'load', function() {

	// Get all forms on page
	var formArray =  $A($(document.getElementsByTagName('form')));

    // Loop thru forms, register any forms which do NOT have the CSS
    // class "ignoreForm"
	formArray.each(function(aForm) {

    	if (!($(aForm).hasClassName('ignoreForm'))) {
        	new FormWatch(aForm);
	  }

  });

  // Get all elements with a ignoreClick
  var linkArray = $A($(document.getElementsBy))

  });